let Vue = null;
function install(_vue) {
  Vue = _vue
  Vue.mixin({
    beforeCreate() {
      if (this.$options.router) {
        this.$router = this.$options.router
      } else if (this.$parent) {
        this.$router = this.$parent.$router
      }
    },
  })
  Vue.component('router-link', {
    props: ['to'],
    render(h) {
      // h --> createElement
      return h('a', {
        attrs: {
          href: '#' + this.to
        }
      }, this.$slots.default)
    }
  })
  Vue.component('router-view', {
    render(h) {
      //  怎么获取要去渲染的组件？？？
      // return h('h1', {}, 'hello')
      return h(this.$router.routerMap[this.$router._route])
    }
  })
}
class VueRouter {
  // static install = install
  constructor(options) {
    let routes = options.routes;// 路由映射表
    //  做出一个对象  让路径作为属性名  组件对象作为属性值
    this.routerMap = {};
    routes.forEach(item => {
      this.routerMap[item.path] = item.component
    })
    Vue.util.defineReactive(this, '_route', '/home');// 劫持router实例行的_route属性

    this._route = location.hash.slice(1)
    window.onhashchange = () => {
      this._route = location.hash.slice(1)
      console.log(location.hash)
    }
  }
}
VueRouter.install = install

export default VueRouter